<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if ($this_city != 0)
{
	$companies = $db->getTable("select `C`.`company_id`,`C`.`name`,`C`.`head_office`,`C`.`address`,`C`.`phone`,`city`.`name` as `city_name` from `companies` as `C`,`city` where `C`.`city_id` = '$this_city' and `C`.`city_id` = `city`.`city_id` order by `name`");
	
	$comp_array = array();
	
	$answer = "[";
	foreach ($companies as $key => $company){
		$comp_array['id'][] = $company['company_id'];
		$comp_array['name'][] = $company['name'];
		$answer .= '{"name":"'.mysql_real_escape_string($company['name']).'","address":"'.mysql_real_escape_string($company['address']).'","phone":"'.$company['phone'].'","city":"'.$company['city_name'].'","head":"'.($company['head_office'] == 0 ? "head" : "branche").'"}';
		if ($key < count($companies)-1)
			$answer .= ",";
	}
	$query = "select `A`.`address`,`A`.`phone`,`C`.`name`,`city`.`name` as `city_name` from `additional_office` as `A` left join `companies` as `C` on `C`.`company_id` = `A`.`company_id` left join `city` on `C`.`city_id` = `city`.`city_id` where `A`.`company_id` = '".implode(",",$comp_array['id'])."'";
	$offices = $db->getTable($query);
	foreach ($offices as $office)
		$answer .= ',{"name":"'.mysql_real_escape_string($office['name']).'","address":"'.mysql_real_escape_string($office['address']).'","phone":"'.$office['phone'].'","city":"'.$office['city_name'].'","head":"office"}';
	
	echo $answer .= "]";
}

?>